package com.free.files.nio;

import com.free.common.vo.ResultModel;
import com.free.exception.SystemException;
import com.free.files.IFiler;
import com.free.utils.FreeFileUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
public class NioFiler implements IFiler {

    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/
    private static final IFiler _instance = new NioFiler();
    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    public static IFiler instance(){
        return _instance;
    }


    @Override
    public ResultModel upload(byte[] content, String serverPath) throws SystemException {
        File destFile = newFileAndConfirmPath(serverPath);
        try {
            FileUtils.writeByteArrayToFile(destFile, content);
        } catch (IOException e) {
            throw new SystemException("NioFiler:写文件失败" ,e);
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel upload(String localPath, String serverPath) throws SystemException {
        File file = new File(localPath);
        if(!file.exists()){
            throw new SystemException("NioFiler:源File:" + localPath +"文件不存在!");
        }
        File destFile = newFileAndConfirmPath(serverPath);
        try {
            FileUtils.copyFile(file, destFile);
        } catch (IOException e) {
            throw new SystemException("NioFiler:复制文件失败", e);
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel downLoad(String serverPath) throws SystemException {
        File file = new File(serverPath);
        if(!file.exists()){
            throw new SystemException("NioFiler:源File:" + serverPath +"文件不存在!");
        }
        byte[] bytes = null;
        try {
            bytes = FileUtils.readFileToByteArray(file);
        } catch (IOException e) {
            throw new SystemException("NioFiler:文件内容读取失败", e);
        }
        return ResultModel.success(bytes);
    }

    @Override
    public ResultModel downLoad(String serverPath, String localPath) throws SystemException {
        return upload(serverPath,localPath);
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/

    private File newFileAndConfirmPath(String path) throws SystemException {
        String directory = FreeFileUtils.getFileDirectory(path);
        if(StringUtils.isNotEmpty(directory)){
            File dir = new File(directory);
            if(!dir.exists()){
                boolean mkDirSuccess = dir.mkdirs();
                if(!mkDirSuccess){
                    throw new SystemException("NioFiler:创建目标目录" + path + "失败");
                }
            }
        }
        File destFile = new File(path);
        return destFile;
    }

}
